package com.jbp.ssm.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.jbp.ssm.entity.ApiResult;
import com.jbp.ssm.entity.UserEntity;
import com.jbp.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * Created by david on 2017/6/1.
 *
 * @email davidmaster@163.com
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping(value = "/get"
            ,method = RequestMethod.GET)
    @ResponseBody
    public ApiResult Get(@RequestParam Integer id){
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id.longValue());
        userEntity.setUserName("david");
        userEntity.setUserPassword("password");
        ApiResult<UserEntity> result = new ApiResult<>();
        result.setCode(0);
        result.setData(userEntity);
        result.setErrMsg("");
        return result;
    }
    @RequestMapping(path = "/delete",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult Delete(@RequestParam Integer id){
        ApiResult<Boolean> result = new ApiResult<>();
        result.setCode(0);
        result.setData(true);
        result.setErrMsg(id.toString());
        return result;
    }
    @RequestMapping(path = "/insert",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult Insert(@RequestBody UserEntity user){
        user.setId(100L);
        ApiResult<UserEntity> result=new ApiResult<>();
        result.setCode(0);
        result.setData(user);
        if(user.getUserName().equalsIgnoreCase("david"))
        result.setErrMsg("插入成功");
        else
            result.setErrMsg("插入失败");
        return result;
    }
    @RequestMapping(path = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult List(){
        ApiResult<PageInfo> result = new ApiResult<>();
        result.setCode(0);
        Page<UserEntity> page = new Page<>(1,10);
        UserEntity userEntity = new UserEntity();
        userEntity.setId(100L);
        userEntity.setUserName("david");
        userEntity.setUserPassword("password");
        page.add(userEntity);
        page.add(userEntity);
        PageInfo info = page.toPageInfo();
        result.setData(info);
        return result;
    }
    @RequestMapping(path = "/query",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult Query(){
        ApiResult<List<UserEntity>> result = new ApiResult<>();
        result.setCode(0);
        result.setErrMsg("---");
        Map<String,Object> map = new HashMap<>();
        map.put("offset",0);
        map.put("limit",10);
        result.setData(userService.queryList(map));
        return result;

    }
    @RequestMapping(path = "/init",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult Init(@RequestParam Integer count){
        ApiResult<Long[]> result = new ApiResult<>();
        Long[] data=new Long[count];
        for (int i=0;i<count;i++){
            UserEntity userEntity = new UserEntity();
            userEntity.setUserName(getRandomString(5));
            userEntity.setUserPassword(getRandomString(20));
            userEntity.setIsActive(true);
            userService.insert(userEntity);

            data[i]=userEntity.getId();
        }

        result.setData(data);
        return result;

    }
    private static String getRandomString(int length){
        String base="abcdefghijklmnopqrstuvwxyz0123456789";
        StringBuffer stringBuffer = new StringBuffer(length);
        Random random=new Random();
        for (int i=0;i<length;i++){
            int number=random.nextInt(base.length());
            stringBuffer.append(base.charAt(number));
        }
        return stringBuffer.toString();
    }
}
